#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "list.h"
#include "service.h"

struct list* head;

void* send_msg(void* arg);
void All_send(struct list *head,char msg[]);
void* recv_msg(void* arg);

int main(int argc, char const *argv[])
{
    head = Create_Head();
    if(head == NULL) return -1;
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == -1)
    {
        perror("socket");
        return -1;
    }
    perror("socket");

    struct sockaddr_in addr;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8848);

    int ret_val = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
    if(ret_val == -1)
    {
        perror("bind");
        close(sockfd);
        return -1;
    }
    perror("bind");

    ret_val = listen(sockfd, 10);
    if(ret_val == -1)
    {
        perror("listen");
        close(sockfd);
        return -1;
    }
    perror("listen");

    struct sockaddr_in client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    while(1)
    {
        int clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &client_addr_len);
        if(clientfd == -1)
        {
            perror("accept");
            continue;
        }
        std::cout << "新用户"<<clientfd<<"来自"\
                  <<inet_ntoa(client_addr.sin_addr)\
                  <<":"<<ntohs(client_addr.sin_port)<<"已连接" << std::endl;
        int* fd = (int *)malloc(sizeof(int));
        *fd = clientfd;
        struct list *node = Add_User(head,(void *)fd);
        pthread_t send_tid, recv_tid;
        pthread_create(&send_tid, NULL, send_msg, (void *)node);
        pthread_create(&recv_tid, NULL, recv_msg, (void *)node);
        pthread_detach(send_tid);
        pthread_detach(recv_tid);
    }

    close(sockfd);
    return 0;
}

void* send_msg(void* arg)
{
    struct list *node = (struct list *)arg;
    int clientfd = node->sockfd;
    char msg[128];
    while(1)
    {
        if(fgets(msg, sizeof(msg), stdin) == NULL)
        {
            perror("fgets");
            break;
        }
        All_send(head,msg);
    }
    return NULL;
}

void All_send(struct list *head,char msg[])
{
    struct list *tmp = head->next;
    int count = 0;
    int ret_val = 0;
    while(tmp != head)
    {
        count++;
        tmp = tmp->next;
    }
    tmp = head->next;
    for(int i = 0;i < count;i++)
    {
        ret_val = send(tmp->sockfd,msg,strlen(msg),0);
        if(ret_val < 0)
        {
            perror("send");
            break;
        }
        std::cout << "发送信息" << ret_val << "字节给"<<tmp->sockfd<<"用户" << std::endl;
        tmp = tmp->next;
    }
}

void* recv_msg(void* arg)
{
    struct list *node = (struct list *)arg;
    int clientfd = node->sockfd;
    char msg[128];
    int ret_val = -1;
    while(1)
    {
        bzero(msg, sizeof(msg));
        ret_val = recv(clientfd, msg, sizeof(msg), 0);
        if(ret_val == 0)
        {
            std::cout << "用户"<<node->id <<"断开连接" << std::endl;
            std::cout << "在线用户编号:" << std::endl;
            Remove_User(head,(void *)&node->sockfd);
            Display_Node(head);
            break;
        }
        else if(ret_val < 0)
        {
            perror("recv");
            break;
        }
        else
        {
            std::cout << "接收到" <<"来自用户"<<node->sockfd<<"的"\
            << ret_val <<"字节数据：" << msg << std::endl;
        }
    }
    close(clientfd);
    return NULL;
}